<template>
    <div>
        <el-dialog :title="`上传${setBtnText}文件`" center @close="sendAnswerClose" :visible.sync="showSendAnswer" class="sendAnswer">
            <el-upload
                class="upload"
                :action="iploadULR"
                :on-remove="handleRemove"
                :on-change="handleChange"
                :http-request="uploadSectionFile"
                multiple
                :auto-upload="true"
                :limit="1"
                :file-list="fileList">
                <el-button size="small" type="success">{{`选择${setBtnText}文件`}}</el-button>
                <div slot="tip" class="el-upload__tip">最多只能上传一个文件,如果有多个文件,请将文件夹压缩后上传</div>

            </el-upload>
            <el-button size="small" type="primary"  :loading="sendAnswerLoading" @click="submitAnswerFile" class='btn-send' v-no-more-click:3000>{{`发送${setBtnText}文件`}}</el-button>
        </el-dialog>
    </div>
</template>

<script>
import axios from "axios"
export default {
    data() {
        return {
            showSendAnswer: false,
            setBtnText: '答复',
            iploadULR: '',
            fileListV: [],
            fileList: [],
            sendAnswerLoading: false,
            finalAnswer: null,
            baseUrl: global.baseurl,
        }
    },

    methods: {
        showOn(data) {
            this.finalAnswer = data.id
            if (data.status == '等待答复') {
                this.setBtnText = '答复'
            } else {
                this.setBtnText = '补正'
            }
            this.showSendAnswer = true
        },
        sendAnswerClose () {
            this.fileList = []
            this.fileListV = []
        },
        handleRemove(file, fileList) {
			this.fileListV = fileList
        },
        handleChange(file, fileList) {
            let tempList = [];//可以上传的文件
            let that = this;
            let canUpload = true;//是否可以上传
            let fileName,
                fileNameArr,
                arrLen,
                pos,
                lastName,
                orderNum =''
            for(let i = 0;i<fileList.length;i ++){
                // //验证文件类型
                fileName = fileList[i].name;
                pos = fileName.lastIndexOf(".")
                lastName = fileName.substring(pos, fileName.length)
                tempList.push(fileList[i])
            }
            this.fileListV.splice(0,this.fileListV.length,...tempList)
            fileList.splice(0, fileList.length,...tempList)
            return
        },
        uploadSectionFile(params) {
        },
        submitAnswerFile () {
            this.sendAnswerLoading = true
            let list = document.getElementsByClassName(
                "el-upload-list__item is-ready"
            );
            if (list.length == 0) {
                this.$message({
                    type: "warning",
                    message: "请选择需要上传的文件！"
                });
                return
            }
            // FormData 对象
            let form = new FormData()
            // 文件对象
            form.append('finalAnswer', this.finalAnswer)
            form.append('file', this.fileListV[0].raw);

            axios({
                method: "post",
                url: this.baseUrl + `/api/finals/${this.finalAnswer}/agent/my/answer`,
                data: form,
                headers: {
                    "Content-Type": "multipart/form-data",
                    Authorization: "Bearer " + this.$store.state.token
                }
            }).then(res => {
                if (res.data.status == "success") {
                    this.$message({
                        type: "success",
                        message: "上传答复文件成功"
                    })
                    this.$emit('refresh')
                    this.sendAnswerLoading = false
                    this.fileListV = []
                    this.showSendAnswer = false
                } else {
                    this.$message({
                        type: "error",
                        message: res.data.message
                    });
                    this.fileListV = []
                }
            });
        },
    }
}
</script>